package modules

import (
	"fmt"
	"service/components"
	"time"

	"gorm.io/gorm"
)

type File struct {
	Id        int64  `json:"id" form:"id" gorm:"primaryKey;comment:id主键"`
	Url       string `json:"url" form:"url" gorm:"size:256;"`
	UUID      string `json:"uuid" form:"uuid" gorm:"size:256;comment:唯一标识"`
	Type      string `json:"type" form:"type" gorm:"size:128"`
	FileName  string `json:"file_name" form:"file_name"  gorm:"size:128;comment:文件的名字"`
	Attribute string `json:"attribute" form:"attribute" gorm:"size:128;comment:文件的属性"`
	Desc      string `json:"desc" form:"desc" gorm:"type:text;comment:文件的描述"`

	CreateAt  time.Time      `json:"create_at" form:"create_at"`
	DeletedAt gorm.DeletedAt `json:"deleted_at" form:"deleted_at"`
}

func (f File) TableName() string {
	return "file"
}

func AddFilePictureService(file *File) error {
	err := components.DB.Model(File{}).Create(file).Error
	return err
}

func GetAllPicsService() ([]File, int64) {
	var file []File
	count := components.DB.Model(File{}).Order("id desc").Find(&file).RowsAffected
	return file, count
}

// GetFileByUUIDService 根据UUID查找文件
func GetFileByUUIDService(uuid string) ([]*File, error) {
	var c []*File
	str := fmt.Sprintf("uuid = '%s'", uuid)
	err := components.DB.Model(File{}).Where(str).First(&c).Error
	return c, err
}
